<template>
  <div class="payZoneList">
    <section class="header clearfix">
      <i class="iconfont iconfanhui1" @click="goBack()"></i>
      <span @click="goBack()">区域缴纳</span>
      <span class="iconfont iconadd1" @click="add"> 添加</span>
    </section>
    <div class="search-box">
      <section class="option">
        <span>区域缴纳项目:</span>
        <el-select v-model="areaPaymentId" @change="getData()" placeholder="请选择">
          <el-option
            v-for="item in areaPaymentList"
            :key="item.areaPaymentId"
            :label="item.paymentName"
            :value="item.areaPaymentId"
          ></el-option>
        </el-select>
      </section>
      <section class="option">
        <span>缴纳类型:</span>
        <el-select v-model="paymentType" @change="getData()" placeholder="请选择">
          <el-option
            v-for="item in paymentTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </section>
    </div>
    <div class="table">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="companyPaymentId" label="缴纳项目记录ID"></el-table-column>
        <el-table-column prop="paymentName" label="缴纳项目"></el-table-column>
        <el-table-column
          prop="paymentType"
          label="缴纳类型"
          :formatter="typeFormatter"
        ></el-table-column>
        <el-table-column
          prop="paymentItemType"
          label="项目类别"
          :formatter="paymentTypeIdFilter"
        ></el-table-column>
        <el-table-column
          prop="basic"
          label="基数"
          :formatter="baseValFormatter"
        ></el-table-column>
        <el-table-column
          prop="defaultVal"
          label="默认值"
          :formatter="baseValFormatter"
        ></el-table-column>
        <el-table-column
          prop="companyVal"
          label="公司份额"
          :formatter="valueFormatter"
        ></el-table-column>
        <el-table-column
          prop="personVal"
          label="个人份额"
          :formatter="valueFormatter"
        ></el-table-column>
        <el-table-column label="详情">
          <template slot-scope="scope">
            <el-button size="mini" @click="edit(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="remove(scope.row.companyPaymentId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="current"
        :page-sizes="[6, 12, 24, 36]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!--新增对话框-->
    <el-dialog
      title="新增缴纳项目"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      center
    >
      <el-form :model="form">
        <el-form-item label="区域代码" :label-width="formLabelWidth">
          <el-select
            v-model="areaCode"
            filterable
            @change="areaCodeChange"
            placeholder="请选择"
          >
            <el-option
              v-for="item in areaCodeOptions"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缴纳项目" :label-width="formLabelWidth">
          <el-select
            v-model="form.areaPaymentId"
            @change="getAreaPaymentDetail"
            placeholder="请选择"
          >
            <el-option
              v-for="item in areaPaymentOptions"
              :key="item.areaPaymentId"
              :label="item.paymentName"
              :value="item.areaPaymentId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="缴纳方式" :label-width="formLabelWidth">
          <el-select
            v-model="form.paymentType"
            @change="typeChange"
            placeholder="请选择"
            disabled
          >
            <el-option
              v-for="item in paymentTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.paymentType == 0"
          label="默认值"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.defaultVal"
            clearable
            placeholder="请输入默认值"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item
          label="基数"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.basic" clearable placeholder="请输入基数"
            ><template slot="append">元</template></el-input
          >
        </el-form-item>
        <p class="tip" v-if="form.paymentType == 1">
          提示：基数取值范围必须在{{ form.baseValMin }}~{{ form.baseValMax }}之间
        </p>
        <el-form-item
          v-if="form.paymentType == 1"
          label="公司比例"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.companyVal"
            suffix-icon="iconfont iconbaifenbi"
            placeholder="请输入公司比例"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="form.paymentType == 1"
          label="个人比例"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.personVal"
            suffix-icon="iconfont iconbaifenbi"
            placeholder="请输入个人比例"
            disabled
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogFormVisible = false;
            addLoading = false;
          "
          >取 消</el-button
        >
        <el-button type="primary" :loading="addLoading" @click="confirmAdd()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!--修改对话框-->
    <el-dialog
      title="编辑缴纳项目"
      :visible.sync="dialogEdit"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      center
    >
      <el-form :model="form">
        <el-form-item label="缴纳项目" :label-width="formLabelWidth">
          <el-select v-model="form.areaPaymentId" disabled placeholder="请选择">
            <el-option
              v-for="item in areaPaymentOptions"
              :key="item.areaPaymentId"
              :label="item.paymentName"
              :value="item.areaPaymentId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缴纳方式" :label-width="formLabelWidth">
          <el-select
            v-model="form.paymentType"
            @change="typeChange"
            disabled
            placeholder="请选择"
          >
            <el-option
              v-for="item in paymentTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.paymentType == 0"
          label="默认值"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.defaultVal"
            disabled
            placeholder="请输入默认值"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="基数"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.basic" clearable placeholder="请输入基数"
            ><template slot="append">元</template></el-input
          >
        </el-form-item>
        <p class="tip" v-if="form.paymentType == 1">
          提示：基数取值范围必须在{{ form.baseValMin }}~{{ form.baseValMax }}之间
        </p>
        <el-form-item
          label="公司比例"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.companyVal"
            suffix-icon="iconfont iconbaifenbi"
            placeholder="请输入公司比例"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item
          label="个人比例"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.personVal"
            suffix-icon="iconfont iconbaifenbi"
            placeholder="请输入个人比例"
            disabled
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogEdit = false;
            updateLoading = false;
          "
          >取 消</el-button
        >
        <el-button type="primary" :loading="updateLoading" @click="confirmUpdate()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import base from "@/assets/js/base";
import { requestApiPost } from "../api/api";
import commonData from "@/assets/js/commonData";
import { reqRep } from "../report/reqReport";
import { resRep } from "../report/resReport";
export default {
  name: "payZoneList",
  data() {
    return {
      formLabelWidth: "160px",
      paymentType: "",
      areaPaymentId: "",
      noData: true,
      cityCode: "",
      areaData: {},
      tableData: [],
      current: 1,
      pageSize: 6,
      total: 0,
      paymentTypeOptions: [
        {
          value: "0",
          label: "值",
        },
        {
          value: "1",
          label: "比例",
        },
      ],
      paymentTypeList: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "0",
          label: "值",
        },
        {
          value: "1",
          label: "比例",
        },
      ],
      areaPaymentOptions: [],
      areaPaymentList: [],
      dialogFormVisible: false,
      addLoading: false,
      updateLoading: false,
      dialogEdit: false,
      form: {},
      areaCode: "",
      areaCodeOptions: [],
      itemClassification: [],
    };
  },
  methods: {
    handleCurrentChange(e) {
      this.current = e;
      this.getData();
    },
    handleSizeChange(e) {
      this.pageSize = e;
      this.getData();
    },
    //选择缴纳方式
    typeChange(e) {
      if (e == 0) {
        this.form.baseVal = "";
        this.form.companyVal = "";
        this.form.personVal = "";
      } else {
        this.form.defaultVal = "";
      }
    },
    //选择城市
    areaCodeChange() {
      this.$set(this.form, "areaPaymentId", "");
      this.getAreaPayment();
    },
    //新增
    add() {
      this.areaCode = "";
      this.$set(this.form, "areaPaymentId", "");
      this.$set(this.form, "basic", "");

      this.$set(this.form, "paymentType", "");
      this.$set(this.form, "defaultVal", "");
      this.$set(this.form, "companyVal", "");
      this.$set(this.form, "personVal", "");

      this.dialogFormVisible = true;
    },
    confirmAdd() {
      if (!this.form.areaPaymentId || !this.form.paymentType) {
        this.$message.warning("信息不完整");
        return;
      }
      if (
        this.form.paymentType == 1 &&
        (Number(this.form.basic) < Number(this.form.baseValMin) ||
          Number(this.form.basic) > Number(this.form.baseValMax))
      ) {
        this.$message.warning("基数错误，请输入范围中的值");
        return;
      }
      this.$set(this.form, "companyId", this.$route.query.companyId);

      let data = JSON.parse(JSON.stringify(this.form));

      this.addLoading = true;
      let reqReport = reqRep({
        traCode: "_ADD_COMPANY_PAYMENT",
        data: data,
      });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.addLoading = false;
        } else {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.dialogFormVisible = false;
          this.addLoading = false;
          this.getData();
        }
      });
    },
    //编辑
    edit(info) {
      console.log(info);
      this.form = JSON.parse(JSON.stringify(info));
      this.getAreaPayment();
      //  this.getAreaPaymentDetail(info.areaPaymentId);
      this.dialogEdit = true;
    },
    //确认编辑
    confirmUpdate() {
      if (
        this.form.paymentType == 1 &&
        (Number(this.form.basic) < Number(this.form.baseValMin) ||
          Number(this.form.basic) > Number(this.form.baseValMax))
      ) {
        this.$message.warning("基数错误，请输入范围中的值");
        return;
      }

      let data = JSON.parse(JSON.stringify(this.form));

      this.updateLoading = true;
      let reqReport = reqRep({
        traCode: "_EDIT_COMPANY_PAYMENT",
        data: data,
      });

      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.updateLoading = false;
        } else {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.dialogEdit = false;
          this.updateLoading = false;
          this.getData();
        }
      });
    },
    //删除
    remove(id) {
      this.$confirm("确定删除该区域缴纳项目?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.removePayment(id);
        })
        .catch(() => {});
    },
    removePayment(id) {
      var reqData = {
        companyPaymentId: id,
      };
      let reqReport = reqRep({
        traCode: "_DEL_COMPANY_PAYMENT",
        data: reqData,
      });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        }
      });
    },
    getData() {
      this.loading = true;
      var reqData = {
        companyId: this.$route.query.companyId,
        areaPaymentId: this.areaPaymentId,
        current: this.current,
        pageSize: this.pageSize,
      };
      if (this.paymentType) {
        reqData.paymentType = this.paymentType;
      }
      let reqReport = reqRep({
        traCode: "_LIST_COMPANY_PAYMENT",
        data: reqData,
      });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.loading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.tableData = resReport.data.records;
          this.total = parseInt(resReport.data.total);
        }
      });
    },
    //获取区域缴纳项目列表
    getAreaPayment() {
      var reqData = {
        paymentName: "",
        areaCode: this.areaCode,
        current: 1,
        pageSize: 999,
      };
      let reqReport = reqRep({ traCode: "_LIST_AREA_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.areaPaymentOptions = JSON.parse(JSON.stringify(resReport.data.records));
          if (this.first) {
            this.areaPaymentList = JSON.parse(JSON.stringify(resReport.data.records));
            this.areaPaymentList.unshift({
              areaPaymentId: "",
              paymentName: "全部",
            });
            this.first = false;
            this.getData();
          }
        }
      });
    },
    //获取区域缴纳项目详情
    getAreaPaymentDetail(areaPaymentId) {
      var reqData = {
        areaPaymentId: areaPaymentId,
      };
      let reqReport = reqRep({ traCode: "_GET_AREA_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          let info = resReport.data;
          this.form.paymentType = info.paymentType;
          //比例
          if (info.paymentType == 1) {
            this.form.baseValMax = info.baseValMax;
            this.form.baseValMin = info.baseValMin;
            this.form.basic = info.baseVal;
            this.form.companyVal = info.companyScale;
            this.form.personVal = info.personScale;
          } else {
            this.form.defaultVal = info.defaultVal;
          }
        }
      });
    },
    //获取区域列表
    getAreaCode() {
      var reqData = {
        grade: 1,
        isStart: 0,
        current: 1,
        pageSize: 999,
      };
      let reqReport = reqRep({ traCode: "_LIST_AREA", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.areaCodeOptions = resReport.data.records;
          this.getAreaPayment();
        }
      });
    },
    goBack() {
      this.$router.go(-1);
    },
    typeFormatter(a, b, c) {
      if (c == 0) {
        return "值";
      } else {
        return "比例";
      }
    },
    valueFormatter(a, b, c) {
      if (c) {
        return c + "%";
      } else {
        return "-";
      }
    },
    baseValFormatter(a, b, c) {
      if (c >= 0) {
        return c;
      } else {
        return "-";
      }
    },
    paymentTypeIdFilter(a, b, c) {
      return this.itemClassification[c].label;
    },
  },
  created() {
    this.first = true;
    this.itemClassification = commonData.itemClassification;
    this.getAreaCode();
  },
};
</script>
<style scoped>
.header {
  padding: 15px;
  height: 50px;
  box-sizing: border-box;
  font-size: 14px;
  border-bottom: 1px solid #eee;
}
.header i {
  margin-right: 5px;
  cursor: pointer;
}
.header span {
  cursor: pointer;
}
.header .iconadd1 {
  float: right;
  font-size: 14px;
  color: #13cb6e;
}
.main-content {
  padding: 30px;
}
h2 {
  display: block;
  color: #333;
  font-size: 16px;
  margin: 30px 0;
  clear: both;
}
.input-box {
  float: left;
  /* width: 400px; */
  margin-right: 40px;
  margin-bottom: 20px;
}
.search-box {
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid #eee;
}
.search-box .option {
  display: inline-block;
  margin-left: 15px;
}
.search-box .el-select {
  width: 150px;
}
.search-box .el-select input {
  min-width: 100px;
  background-color: #eee;
}
.nbsp {
  float: none;
  display: block;
}
.input-box .title {
  display: inline-block;
  margin-right: 7px;
  width: 100px;
  color: #666;
}
.input-box .value {
  display: inline-block;
}
.input-box .value input {
  width: 228px;
}
.input-box .value .base {
  width: 106px;
}
.error {
  text-align: center;
}
.opt-user {
  padding: 20px 15px;
}
.opt-user > section {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}
.opt-user i {
  font-size: 20px;
  margin-right: 5px;
  vertical-align: middle;
}
.opt-user .iconadd1 {
  color: #13cb6e;
}
.opt-user .iconbiaoge-xiazai {
  color: #33ac37;
}
.add-user:hover {
  color: #13cb6e;
}
.comfirm {
  color: #fff;
  background-color: #13cb6e;
}
.add {
  color: #fff;
  background-color: #13cb6e;
}
.tip {
  color: #f56c6c;
  font-size: 12px;
  padding-left: 120px;
  text-align: center;
  margin-bottom: 4px;
}
.sm-f {
  padding: 30px;
}
.sm-f > ul {
  display: inline-block;
  margin-right: 15px;
  color: #666;
}
.sm-f .value {
  color: #666;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 5px;
}
.sm-f .no {
  opacity: 0;
}
.sm-f .button {
  float: right;
  width: 140px;
  text-align: center;
  height: 50px;
  font-size: 20px;
  line-height: 50px;
  color: #fff;
  cursor: pointer;
  border-radius: 4px;
  background-color: #13cb6e;
}
.footer {
  padding: 10px 30px 30px;
  color: #999;
}
.footer span {
  margin-right: 50px;
}
.hint {
  padding: 0 30px 10px;
  color: #999;
}

.nodata {
  color: #999;
  font-size: 14px;
}
.table .content {
  display: inline-block;
  border-right: 1px solid #ccc;
}
.table .content:last-child {
  border: none;
}
.table .content > section {
  padding: 10px 15px;
}
.table .content > section span {
  margin-right: 10px;
  display: inline-block;
  width: 80px;
  color: #999;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.table .content > section span:nth-child(2) {
  text-align: center;
}
.table .content > section span:last-child {
  margin-right: 0;
  text-align: center;
}
</style>
